//
//  MeViewController.m
//  MyBlog
//
//  Created by NCS on 16/12/16.
//  Copyright © 2016年 YSL. All rights reserved.
//

#import "MeViewController.h"

@interface MeViewController ()

@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) NSMutableArray *listData;

@end

@implementation MeViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    //修改NavigaionBar的高度
    CGFloat navigationWidth = self.navigationController.navigationBar.frame.size.width;
    self.navigationController.navigationBar.frame = CGRectMake(0, 20, navigationWidth, 44 * 3);
//    [self printViewHierarchy:self.navigationController.navigationBar];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *titleView = [[UIView alloc] init];
    self.navigationItem.titleView = titleView;
    
    self.headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header"]];
    self.headerImageView.layer.cornerRadius = 35;
    self.headerImageView.layer.masksToBounds = YES;
    self.headerImageView.frame = CGRectMake(0, 0, 70, 70);
    self.headerImageView.center = CGPointMake(self.navigationItem.titleView.center.x, 0);
    
    [titleView addSubview:self.headerImageView];
    
    // Init data
    self.listData = [[NSMutableArray alloc] init];
    for (int i = 0; i < 100; i++) {
        [self.listData addObject:[NSString stringWithFormat:@"Item %d", i]];
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
        
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)printViewHierarchy:(UIView *)superView {
    static uint level = 0;
    for(uint i = 0; i < level; i++){
        printf("\t");
    }
    
    const char *className = NSStringFromClass([superView class]).UTF8String;
    const char *frame = NSStringFromCGRect(superView.frame).UTF8String;
    printf("%s:%s\n", className, frame);
    
    ++level;
    for(UIView *view in superView.subviews){
        [self printViewHierarchy:view];
    }
    --level;
}

- (void)changeWidth:(CGFloat)width height:(CGFloat)height scale:(CGFloat)scale{
    self.navigationController.navigationBar.frame = CGRectMake(0, 20, width, height * scale);
    [self.tableView setFrame:CGRectMake(0, height * scale, self.tableView.frame.size.width, self.tableView.frame.size.height)];
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y + scrollView.contentInset.top;
    
    CGFloat scaleY = 1.0;
    CGFloat scaleX = 1.0;
    CGFloat scaleNav = 3.0;
    CGRect naviCenter = self.navigationItem.titleView.frame;
    
    NSLog(@"%f", naviCenter.origin.x);
    // 放大
    if (offsetY < 0) {
        // 允许下拉放大的最大距离为300
        // 1.5是放大的最大倍数，当达到最大时，大小为：1.5 * 70 = 105
        // 这个值可以自由调整
        // X轴的比例按照Y轴的来变化
        scaleY = MIN(1, 1 - offsetY / 300);
        scaleX = MIN(1, 1 - offsetY / 300);
        scaleNav = MIN(1, 3 - offsetY / 300);
    } else if (offsetY > 0) { // 缩小
        // 允许向上超过导航条缩小的最大距离为300
        // 为了防止缩小过度，给一个最小值为0.45，其中0.45 = 31.5 / 70.0，表示
        // 头像最小是31.5像素
        // X轴的比例按照Y轴的来变化，最小值设置为0.15，防止图片移出屏幕
        scaleY = MAX(0.45, 1 - offsetY / 300);
        scaleX = MAX(0.15, 1 - offsetY / 300);
        scaleNav = MAX(1, 3 - offsetY / 300);
    }
    
    self.headerImageView.transform = CGAffineTransformMakeScale(scaleY, scaleY);
    
    // Navigation 的坐标是以center为原点，上和左为负，下和右为正
    CGRect frame = self.headerImageView.frame;
    
    // Y轴变化
    frame.origin.y = -self.headerImageView.layer.cornerRadius / 2;
    
    // X轴变化
    CGRect rectNav = self.navigationController.navigationBar.frame;
    CGFloat rectNavWidth = rectNav.size.width;
//    CGFloat headerImageRadius = self.headerImageView.layer.cornerRadius * scaleY;
//    
//    CGFloat imageX = rectNavWidth * scaleX / 2 - headerImageRadius; // 图片左边与屏幕左边的距离
//    frame.origin.x = -(rectNavWidth / 2 - imageX);
//    
//    NSLog(@"scaleX : %f - scaleY : %f \n headerImageRadius : %f \n imageX : %f \n X : frame.origin.x : %f", scaleX, scaleY, headerImageRadius, imageX, frame.origin.x);
    
    //修改NavigaionBar的高度
//    self.navigationController.navigationBar.frame = CGRectMake(0, 20, rectNavWidth, 44 * scaleNav);
    [self changeWidth:rectNavWidth height:44 scale:scaleNav];
    
//    NSLog(@"\n修改后");
//    [self printViewHierarchy:self.navigationController.navigationBar];
    
    self.headerImageView.frame = frame;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeCell" forIndexPath:indexPath];
    
    cell.textLabel.text = self.listData[indexPath.row];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
